﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Diagnostics;
using System.Collections.ObjectModel;

namespace MedicalInterface.ViewModel
{
    public class AddPatientViewModel : BaseViewModel
    {
        #region variables
        private bool _closeSignal;
        private PatientServiceReference.ServicePatientClient servicePatientClient;
        private ObservableCollection<PatientServiceReference.Patient> _listPatient;
        private string _name;
        private string _firstname;
        private DateTime _birthday;

        #endregion

        #region commandes
        private ICommand _cmd_ok;
        #endregion

        #region getter / setter
        /// <summary>
        /// Date de naissance de la personne
        /// </summary>
        public DateTime Birthday
        {
            get { return _birthday; }
            set { _birthday = value; }
        }

        /// <summary>
        /// Prenom de la personne
        /// </summary>
        public string Firstname
        {
            get { return _firstname; }
            set { _firstname = value; }
        }

        /// <summary>
        /// Nom de la personne
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// command pour OK
        /// </summary>
        public ICommand cmd_ok
        {
            get
            {
                if (_cmd_ok == null)
                {
                    _cmd_ok = new RelayCommand(Confirm, CanOk);
                }
                return _cmd_ok;
            }
            set { _cmd_ok = value; }
        }
        /// <summary>
        /// indique si on doit fermer la fenêtre ou non
        /// </summary>
        public bool CloseSignal
        {
            get { return _closeSignal; }
            set
            {
                if (_closeSignal != value)
                {
                    _closeSignal = value;
                    OnPropertyChanged("CloseSignal");
                }
            }
        }




        #endregion

        /// <summary>
        /// constructeur
        /// </summary>
        public AddPatientViewModel(ref ObservableCollection<PatientServiceReference.Patient> listPatient)
        {
            //init variables
            servicePatientClient = new PatientServiceReference.ServicePatientClient();
            _listPatient = listPatient;
        }

        /// <summary>
        /// Le bouton OK est-il accessible?
        /// </summary>
        /// <param name="parameter">Type Object</param>
        /// <returns>True</returns>
        private bool CanOk(object parameter)
        {
            return true;
        }

        /// <summary>
        /// Valider l'ajout du patient
        /// </summary>
        private void Confirm(object parameter)
        {
            bool result = false;
            PatientServiceReference.Patient patient = new PatientServiceReference.Patient();

            patient.Name = Name;
            patient.Firstname = Firstname;
            patient.Birthday = Birthday;
            try
            {
                result = servicePatientClient.AddPatient(patient);
            }
            catch { }
            _listPatient.Add(patient);
            CloseSignal = true;
        }
    }
}
